#!/bin/bash

launch_install()
{
if [ "x$1" = "x" ]; then
  echo "missing setup dir, install abort"
  exit 1
elif [ ! -f $1/install_env.conf ]; then
  echo "install_env.conf not exist or non regular file in [$1], install abort"
  exit 1
fi
clear
alias ls > /dev/null 2>&1 && unalias ls
XDIR=$1
source $XDIR/install_env.conf
. $XDIR/util/func.sh
# if [ -d $SETUP_LOGDIR ]; then rm -rf $SETUP_LOGDIR; fi 为了保留上次的安装日志和过程数据，不能删除这个目录
if [ ! -d $SETUP_LOGDIR ]; then
  mkdir -p $SETUP_LOGDIR || { echo "mkdir SETUP_LOGDIR=$SETUP_LOGDIR fail, install abort."; exit 1; }
fi
echo "Installation start time : `date +%y/%m/%d` `date +%H:%M:%S`" > $SETUP_LOGFILE
echo "" >> $SETUP_LOGFILE

# ================= 初始参数检查 ==================
# 海云主机的名字必须和本机一致
if [ "$HYRENSERV_HOSTNAME" != "`hostname`" ]; then
  die "parameter HYRENSERV_HOSTNAME is not equal to `hostname`"
fi
# 已经把集群的各配置文件准备好了
if [ ! -f $HADOOP_FILES_DIR/conf/core-site.xml ] || [ ! -f $HADOOP_FILES_DIR/conf/hbase-site.xml ] || [ ! -f $HADOOP_FILES_DIR/conf/hdfs-site.xml ] || [ ! -f $HADOOP_FILES_DIR/conf/mapred-site.xml ] || [ ! -f $HADOOP_FILES_DIR/conf/yarn-site.xml ]; then
  die "Must include files : (core-site.xml, hbase-site.xml, hdfs-site.xml, mapred-site.xml, yarn-site.xml) in [$HADOOP_FILES_DIR/conf]"
fi
# HYRENDATA_HOME 变量设置的值必须是“/HRSDATA”结尾
if [ "x${HYRENDATA_HOME##*/}" != "xHRSDATA" ]; then die "HYRENDATA_HOME=$HYRENDATA_HOME : Must end of '/HRSDATA'"; fi

# 软件使用协议 开始
if [ -n "$FDDOOM" ]; then
  echo "$FDDOOM"|sed "s/HRSLF/\n/g"|base64 -d|more
  #cat $XDIR/LICENSE.zh|more
  echo ""
  echo -en "\\033[1;33m"
  echo -n $"我已看过并接受《用户协议》"
  echo -en "\\033[0;39m"
  echo -n " [y/n] : "
  while true; do 
    read inputWord_Confirm
    case $inputWord_Confirm in 
     "y" | "Y" )
      break
      ;;
      "n" | "N" )
      exit 1
      ;;
      *)
      ;;
    esac
  done
  clear
fi
# 软件使用协议 结束

while true; do
clear
#trap ' echo ""; echo "Warning : Dont do this" ' 2 3 15
echo " "
echo " "
echo -en "\\033[1;33m"
echo "                __ __                  ____              "
echo "               / // /_ _________ ___  / __/__ _____  __  "
echo "              / _  / // / __/ -_) _ \_\ \/ -_) __/ |/ /  "
echo "             /_//_/\_, /_/  \__/_//_/___/\__/_/  |___/   "
echo "                  /___/                                  "
echo " "
echo -en "\\033[0;39m"
echo " "
echo "             NOW : 20`date +%y/%m/%d` `date +%H:%M:%S`"
echo " "
echo "             ==========================================  "
echo "             *    Welcome using HyrenServ platform    *  "
echo "             ------------------------------------------  "
echo "             *    1. interactive install "
echo "             *    2. silent install "
echo "             *    9. uninstall "
echo "             *    0. exit "
echo "             ==========================================  "
echo -n "             Please choice [default 2] : "
# stty -echo
read KeyboardChoice
case $KeyboardChoice in
1)
SETUP_SILENT=""
break
;;

2)
break
;;

9)
. $XDIR/cleanup.sh
cleanup
exit 0
;;

0)
clear
exit 0
;;

Sunrier)
echo
echo "you are root, be careful"
break;
;;

*)
echo "Wrong choice"
sleep 1
;;

esac
done

check_password "HYREN_USERPWD" "$HYREN_USERPWD"
check_password "HRS_PGSQL_DBPAWD" "$HRS_PGSQL_DBPAWD"
if [ "$HYRENPRODUCT_NAME" != "DSP" ] && [ "$HYRENPRODUCT_NAME" != "DXG" ] && [ "$HYRENPRODUCT_NAME" != "GOF" ]; then
  die "HYRENPRODUCT_NAME must be [ DSP / DXG / GOF ]."
fi

# 本机创建海云组
grep "^${HYREN_USER}:" /etc/group  > /dev/null || { groupadd $HYREN_USER; }
grep "^${HYREN_USER}:" /etc/group  > /dev/null || { die "create hyren group [${HYREN_USER}] fail.";}
# 本机创建海云用户
grep "^${HYREN_USER}:" /etc/passwd > /dev/null || { useradd -s /bin/bash -g $HYREN_USER $HYREN_USER; }
grep "^${HYREN_USER}:" /etc/passwd > /dev/null || { die "create hyren user [${HYREN_USER}] fail."; }
echo $HYREN_USER:$HYREN_USERPWD | chpasswd || { die "set hyren user password [${HYREN_USERPWD}] fail."; }

. $XDIR/util/hssh.sh
. $XDIR/util/synchostconf.sh
. $XDIR/util/init_oper.sh
. $XDIR/util/init_env.sh
. $XDIR/util/hava.sh
. $XDIR/util/pgsql.sh
. $XDIR/util/tomcat.sh
if [ "$HYRENPRODUCT_NAME" = "DXG" ]; then
. $XDIR/util/dxg_zk.sh
. $XDIR/util/dxg_kfk.sh
. $XDIR/util/dxg_druid.sh
. $XDIR/util/dxg_tranquility.sh
fi
if [ "$HYRENPRODUCT_NAME" = "DSP" ] || [ "$HYRENPRODUCT_NAME" = "GOF" ]; then
. $XDIR/util/init_hadoop.sh
. $XDIR/util/redis.sh
. $XDIR/util/dsp_gof_fenfa_jars.sh
. $XDIR/util/dsp_gof_solr.sh
. $XDIR/util/dsp_gof_server_recv_agent.sh
. $XDIR/util/tomcat_update.sh
. $XDIR/util/dsp_gof_sparkthrift.sh
. $XDIR/util/dsp_gof_phoenix.sh
fi

echo_info "Starting install ... ..."
# 把当前的安装目录下的所有文件赋权给海云用户
chown -R $HYREN_USER:$HYREN_USER $XDIR
chown -R $HYREN_USER:$HYREN_USER $SETUP_LOGDIR

mkdir_check $HYRENTMP_HOME
mkdir_check $HYRENSERV_HOME
mkdir_check $HYRENSOFT_HOME
#mkdir_check $HYRENDATA_HOME
chown -R $HYREN_USER:$HYREN_USER $HYRENTMP_HOME
chown -R $HYREN_USER:$HYREN_USER $HYRENSERV_HOME
chown -R $HYREN_USER:$HYREN_USER $HYRENSOFT_HOME
chown -R $HYREN_USER:$HYREN_USER $HYRENDATA_HOME

echo "set setup dir [$XDIR] and setup log dir [$SETUP_LOGDIR] to $HYREN_USER" >> $SETUP_LOGFILE

# 检查各主机和端口是否可用、在海云主机上安装各种必须使用的软件包（gcc、unzip等）
echo_start "init software env ... ..."
confirm_operate "init software env"
if [ $? -eq 0 ]; then
  echo_info "check hosts conf ... ..."
  local check_host_ok="true"
  check_host "$HYREN_ALLHOSTS" "all host" || { check_host_ok="false"; }
  if [ "$HYRENPRODUCT_NAME" = "DXG" ]; then
    check_host "$HRS_ZOOKEEPER_HOSTS" "zookeeper" || { check_host_ok="false"; }
    check_host "$HRS_KAFKA_HOSTS" "kafka" || { check_host_ok="false"; }
  fi
  if [ "$HYRENPRODUCT_NAME" = "DSP" ] || [ "$HYRENPRODUCT_NAME" = "GOF" ]; then
    check_host "$HRS_REDIS_HOST" "redis" || { check_host_ok="false"; }
    check_host "$HRS_SOLRSTART_HOST" "solr start host" || { check_host_ok="false"; }
    check_host "$HRS_SOLR_HOSTS" "solr service host" || { check_host_ok="false"; }
    check_host "$HRS_SPARKTHRIFT_HOST" "spark-thrift" || { check_host_ok="false"; }
    check_host "$HRS_PHOENIX_HOST" "phoenix" || { check_host_ok="false"; }
  fi
  #check_host "$HRS_PGSQL_HOST" "dbserver"
  #check_host "$HRS_TOMCAT_HOST" "webserver"
  # 上面的主机检查出错了，需要再次确认是否继续安装（由于主机检查用的是ping，如果客户关闭了ping，则可以继续）
  if [ "$check_host_ok" != "true" ]; then
    confirm_operate "somthing wrong in host checking, do you want to continue ?" "n"
    if [ $? -ne 0 ]; then
      die "Setup Abort! "
    fi
  fi

  echo ""
  echo_doing "check system tools ... ..."
  check_tools
  echo_done
  yum_install base64
  yum_install gcc
  yum_install make
  yum_install zip
  yum_install unzip
  yum_install expect
  echo_passed "init software env done."
else
  echo_passed "init software env SKIP"
fi

set_sshnopwd "init ssh"

sync_host_confs "Sync etc hosts file"

init_oper "Init hyren user"

init_env "Init ENV"

setup_java "Installing Java"

if [ "$HYRENPRODUCT_NAME" = "DXG" ]; then
  setup_zookeeper "Installing ZOOKEEPER"
  setup_kafka "Installing KAFKA"
  setup_druid "Installing DRUID"
  setup_tranquility "Installing TRANQUILITY"
fi

setup_pgsql "Installing dbserver"

if [ "$HYRENPRODUCT_NAME" = "DSP" ] || [ "$HYRENPRODUCT_NAME" = "GOF" ]; then
  fenfa_jars "Fenfa Jars"
fi

setup_tomcat "Installing webserver"

if [ "$HYRENPRODUCT_NAME" = "DSP" ] || [ "$HYRENPRODUCT_NAME" = "GOF" ]; then
  init_hadoop "Init hadoop for hyren"
  server_recv_agent "Deploying Server RecvAgent"
  setup_solr "Installing SOLR"
  setup_redis "Installing Redis"
  #tomcat_update "Update webserver For Hadoop"
  setup_spark_thrift "Installing Spark-thrift"
  setup_phoenix "Installing Phoenix"
fi

echo ""
echo_success "Hyrenserv product installing finished."
echo_success "installing info log to [$SETUP_LOGFILE]."
echo ""

echo "Installation end   time : `date +%y/%m/%d` `date +%H:%M:%S`" >> $SETUP_LOGFILE
echo "" >> $SETUP_LOGFILE

echo ""
echo_warn "NOTICE ! please delete all of setup files !"
echo_warn "NOTICE ! please Bring TWO files back to the company : install_env.conf AND $SETUP_LOGFILE"
echo ""

# 清理
local value_arr=($HYREN_ALLHOSTS)
for i in ${value_arr[@]}; do
  # 清空安装过程中使用的临时目录
  ssh -o StrictHostKeyChecking=no $i "if [ -d $HYRENTMP_HOME ]; then rm -rf $HYRENTMP_HOME; fi"
done
value_arr=($HRS_DRUID_HOSTS)
for i in ${value_arr[@]}; do
  # 清空安装过程中使用的临时目录
  ssh -o StrictHostKeyChecking=no $i "if [ -d $HYRENTMP_HOME ]; then rm -rf $HYRENTMP_HOME; fi"
done
ssh -o StrictHostKeyChecking=no $HRS_REDIS_HOST "if [ -d $HYRENTMP_HOME ]; then rm -rf $HYRENTMP_HOME; fi"
ssh -o StrictHostKeyChecking=no $HRS_SPARKTHRIFT_HOST "if [ -d $HYRENTMP_HOME ]; then rm -rf $HYRENTMP_HOME; fi"
ssh -o StrictHostKeyChecking=no $HRS_PHOENIX_HOST "if [ -d $HYRENTMP_HOME ]; then rm -rf $HYRENTMP_HOME; fi"
}
